package book;
class Name{
    String name;

    public Name(String name) {
        this.name = name;
    }
}

public class String_1 {
    public static void main(String[] args) {
        StringBuilder h = new StringBuilder("hadage");
        h.append("HADAGE");
        System.out.println(h);//hadageHADAGE
        h.insert(h.indexOf("ha"), "HAHA");//HAHAhadageHADAGE
        System.out.println(h);
        h.delete(h.indexOf("HA"), h.indexOf("ha"));//hadageHADAGE
        System.out.println(h);
        String hh = "hadage" ;
        h.toString();

    }
    public static void mainHAHA(String[] args) {
        //String修改
        String h = "abcd";
        System.out.println(h.hashCode());
        h += 'a';
        System.out.println(h);
        System.out.println(h.hashCode());
    }

    public static void mainHaha(String[] args) {
//        final char[] a = {'q','a'};
//        a[1] = 'p';
//        System.out.println(a[1]);
        //构造，常用的3种
        String one = "Hadage";//直接赋值
        String two = new String("hadage");//new对象赋值
        char[] arr = {'h', 'a', 'd', 'a', 'g', 'e'};
        String three = new String(arr);//使用重复数组初始化
        System.out.println(one == two);
        System.out.println(one.equals(two));
        System.out.println(one.compareTo(three));
    }

    public static void mainHh(String[] args) {

        //字符串截取substring
        //
        String h = "hadage";
        String hh = h.substring(0, 1);//无结束参数那么默认到尾
        System.out.println(hh);//hadage
        String hhh = h.substring(2, 4);//截取从2到4下标的元素
        System.out.println(hhh);//da
        String Hh = "   ha da ge     ";
        System.out.println(Hh);//   ha da ge
        Hh = Hh.trim();
        System.out.println(Hh);//ha da ge
    }

    public static void mainha(String[] args) {
        //字符串拆分spilt
        String h = "hadage";
        String[] hh = h.split("a");//全部拆开h  d   ge
        for (int i = 0; i < hh.length; i++) {
            System.out.println(hh[i]);
        }
        System.out.println();
        String[] hhh = h.split("a", 2);//h  dage
        for (int i = 0; i < hhh.length; i++) {
            System.out.println(hhh[i]);
        }
    }

    public static void mainHHHH(String[] args) {
        //字符替换
        String h = "had#$%age";
        String hh = h.replaceAll("^$", "1");
        String hhhh = h.replace("$", "1");
        System.out.println(hh);//hAdAge
        System.out.println(hhhh);//hAdAge
    }

    public static void mainHHH(String[] args) {
        String h = "hadage";
        String hh = h.replaceAll("a", "A");
        String hhh = h.replace("a", "A");
        String hhhh = h.replaceFirst("a", "A");
        System.out.println(h);
        System.out.println(hh);
        System.out.println(hhh);
        System.out.println(hhhh);


    }

    public static void mainHH(String[] args) {
        String h = String.format("%d %c %s %.3f", 123, 's', "hadage", 12.99);
        System.out.println(h);//123 s hadage 12.990
    }

    public static void mainH(String[] args) {
        String h = "hadage";
        //字符串转数组toCharArray()
        char[] ch = h.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            System.out.print(ch[i]);
        }//hadage
        System.out.println();
        //数组转字符串
        String hh = new String(ch);
        System.out.println(hh);//hadage
    }

    public static void mainhhhh(String[] args) {
        //大小写转换
        String ha = "hadage";
        System.out.println(ha.toUpperCase());//HADAGE
        System.out.println(ha.toLowerCase());//hadage
    }

    public static void mainhhh(String[] args) {
        //字符串->数字
        String h = "1234";
        int G = Integer.parseInt(h);
        Double GG = Double.parseDouble(h);
        System.out.println(G + 1);//1235
        System.out.println(GG);//1234.0
    }

    public static void mainhh(String[] args) {
        //数字->字符串
        String h = String.valueOf(1234);
        String hh = String.valueOf(12.34);
        String hhh = String.valueOf(new Name("hadage"));
        String hhhh = String.valueOf("hadage");
        System.out.println(hhhh);
        System.out.println(hhh);
        System.out.println(hh);
        System.out.println(h);
    }

    public static void mainh(String[] args) {
        //字符串查找
        String H = "hadage";
        //得到指定下标下的字符charAt()
        System.out.println(H.charAt(2));//d
        //indexOf()，查找指定字符，没有的话放回-1，有的话返回第一次出现的位置
        System.out.println(H.indexOf("d"));//2
        System.out.println(H.indexOf("d", 3));//-1
        System.out.println(H.indexOf('c' + 2, 3));//'c'+2=e  5
    }

    public static void mains(String[] args) {
        //equal比较
        Name one = new Name("hadage");
        Name two = new Name("hadage");
        Name three = one;
//        System.out.println(one.equals(two));
//        System.out.println(one.equals(three));
        System.out.println(one.hashCode());
        System.out.println(two.hashCode());
    }
}
